using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
namespace MicroMsg.UI.UserContrl
{
	public static class LowProfileImageLoader
	{
		private class PendingRequest
		{
			public Image Image
			{
				get;
				private set;
			}
			public Uri Uri
			{
				get;
				private set;
			}
			public PendingRequest(Image image, Uri uri_0)
			{
				this.Image = image;
				this.Uri = uri_0;
			}
		}
		private class ResponseState
		{
			public WebRequest WebRequest
			{
				get;
				private set;
			}
			public Image Image
			{
				get;
				private set;
			}
			public Uri Uri
			{
				get;
				private set;
			}
			public ResponseState(WebRequest webRequest, Image image, Uri uri_0)
			{
				this.WebRequest = webRequest;
				this.Image = image;
				this.Uri = uri_0;
			}
		}
		private class PendingCompletion
		{
			public Image Image
			{
				get;
				private set;
			}
			public Uri Uri
			{
				get;
				private set;
			}
			public Stream Stream
			{
				get;
				private set;
			}
			public PendingCompletion(Image image, Uri uri_0, Stream stream)
			{
				this.Image = image;
				this.Uri = uri_0;
				this.Stream = stream;
			}
		}
		private const int WorkItemQuantum = 5;
		private static readonly Thread _thread;
		private static readonly Queue<LowProfileImageLoader.PendingRequest> _pendingRequests;
		private static readonly Queue<IAsyncResult> _pendingResponses;
		private static readonly object _syncBlock;
		private static bool _exiting;
		public static readonly DependencyProperty UriSourceProperty;
		public static bool IsEnabled
		{
			get;
			set;
		}
		public static Uri GetUriSource(Image image_0)
		{
			if (image_0 == null)
			{
				throw new ArgumentNullException("obj");
			}
			return (Uri)image_0.GetValue(LowProfileImageLoader.UriSourceProperty);
		}
		public static void SetUriSource(Image image_0, Uri value)
		{
			if (image_0 == null)
			{
				throw new ArgumentNullException("obj");
			}
			image_0.SetValue(LowProfileImageLoader.UriSourceProperty, value);
		}
		static LowProfileImageLoader()
		{
			LowProfileImageLoader._thread = new Thread(new ParameterizedThreadStart(LowProfileImageLoader.WorkerThreadProc));
			LowProfileImageLoader._pendingRequests = new Queue<LowProfileImageLoader.PendingRequest>();
			LowProfileImageLoader._pendingResponses = new Queue<IAsyncResult>();
			LowProfileImageLoader._syncBlock = new object();
			LowProfileImageLoader.UriSourceProperty = DependencyProperty.RegisterAttached("UriSource", typeof(Uri), typeof(LowProfileImageLoader), new PropertyMetadata(new PropertyChangedCallback(LowProfileImageLoader.OnUriSourceChanged)));
			LowProfileImageLoader._thread.Start();
			Application.get_Current().add_Exit(new EventHandler(LowProfileImageLoader.HandleApplicationExit));
			LowProfileImageLoader.IsEnabled = true;
		}
		private static void HandleApplicationExit(object sender, EventArgs e)
		{
			LowProfileImageLoader._exiting = true;
			if (Monitor.TryEnter(LowProfileImageLoader._syncBlock, 100))
			{
				Monitor.Pulse(LowProfileImageLoader._syncBlock);
				Monitor.Exit(LowProfileImageLoader._syncBlock);
			}
		}
		private static void WorkerThreadProc(object unused)
		{
			Random random = new Random();
			List<LowProfileImageLoader.PendingRequest> list = new List<LowProfileImageLoader.PendingRequest>();
			Queue<IAsyncResult> queue = new Queue<IAsyncResult>();
			while (!LowProfileImageLoader._exiting)
			{
				lock (LowProfileImageLoader._syncBlock)
				{
					if (LowProfileImageLoader._pendingRequests.get_Count() == 0 && LowProfileImageLoader._pendingResponses.get_Count() == 0 && list.get_Count() == 0 && queue.get_Count() == 0)
					{
						Monitor.Wait(LowProfileImageLoader._syncBlock);
						if (LowProfileImageLoader._exiting)
						{
							break;
						}
					}
					IL_CD:
					while (0 < LowProfileImageLoader._pendingRequests.get_Count())
					{
						LowProfileImageLoader.PendingRequest pendingRequest = LowProfileImageLoader._pendingRequests.Dequeue();
						int i = 0;
						while (i < list.get_Count())
						{
							if (list.get_Item(i).Image == pendingRequest.Image)
							{
								list.set_Item(i, pendingRequest);
								pendingRequest = null;
								IL_C1:
								if (pendingRequest != null)
								{
									list.Add(pendingRequest);
									goto IL_CD;
								}
								goto IL_CD;
							}
							else
							{
								i++;
							}
						}
						goto IL_C1;
					}
					while (0 < LowProfileImageLoader._pendingResponses.get_Count())
					{
						queue.Enqueue(LowProfileImageLoader._pendingResponses.Dequeue());
					}
				}
				Queue<LowProfileImageLoader.PendingCompletion> pendingCompletions = new Queue<LowProfileImageLoader.PendingCompletion>();
				int num = list.get_Count();
				int num2 = 0;
				while (0 < num && num2 < 5)
				{
					int num3 = random.Next(num);
					LowProfileImageLoader.PendingRequest pendingRequest2 = list.get_Item(num3);
					list.set_Item(num3, list.get_Item(num - 1));
					list.RemoveAt(num - 1);
					num--;
					if (pendingRequest2.Uri.get_IsAbsoluteUri())
					{
						HttpWebRequest httpWebRequest = WebRequest.CreateHttp(pendingRequest2.Uri);
						httpWebRequest.set_AllowReadStreamBuffering(true);
						httpWebRequest.BeginGetResponse(new AsyncCallback(LowProfileImageLoader.HandleGetResponseResult), new LowProfileImageLoader.ResponseState(httpWebRequest, pendingRequest2.Image, pendingRequest2.Uri));
					}
					else
					{
						string originalString = pendingRequest2.Uri.get_OriginalString();
						Uri uri = originalString.StartsWith("/", 4) ? new Uri(originalString.TrimStart(new char[]
						{
							'/'
						}), 2) : pendingRequest2.Uri;
						StreamResourceInfo resourceStream = Application.GetResourceStream(uri);
						if (resourceStream != null)
						{
							pendingCompletions.Enqueue(new LowProfileImageLoader.PendingCompletion(pendingRequest2.Image, pendingRequest2.Uri, resourceStream.get_Stream()));
						}
					}
					Thread.Sleep(1);
					num2++;
				}
				int num4 = 0;
				while (0 < queue.get_Count() && num4 < 5)
				{
					IAsyncResult asyncResult = queue.Dequeue();
					LowProfileImageLoader.ResponseState responseState = (LowProfileImageLoader.ResponseState)asyncResult.get_AsyncState();
					try
					{
						WebResponse webResponse = responseState.WebRequest.EndGetResponse(asyncResult);
						pendingCompletions.Enqueue(new LowProfileImageLoader.PendingCompletion(responseState.Image, responseState.Uri, webResponse.GetResponseStream()));
						goto IL_2D5;
					}
					catch (WebException)
					{
						goto IL_2D5;
					}
					break;
					IL_2D5:
					Thread.Sleep(1);
					num4++;
				}
				if (0 < pendingCompletions.get_Count())
				{
					Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
					{
						while (0 < pendingCompletions.get_Count())
						{
							LowProfileImageLoader.PendingCompletion pendingCompletion = pendingCompletions.Dequeue();
							if (LowProfileImageLoader.GetUriSource(pendingCompletion.Image) == pendingCompletion.Uri)
							{
								BitmapImage bitmapImage = new BitmapImage();
								try
								{
									bitmapImage.SetSource(pendingCompletion.Stream);
									goto IL_57;
								}
								catch
								{
									goto IL_57;
								}
								goto IL_4A;
								IL_57:
								pendingCompletion.Image.set_Source(bitmapImage);
							}
							IL_4A:
							pendingCompletion.Stream.Dispose();
						}
					});
				}
			}
		}
		private static void OnUriSourceChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			Image image = (Image)dependencyObject_0;
			Uri uri = (Uri)dependencyPropertyChangedEventArgs_0.get_NewValue();
			if (LowProfileImageLoader.IsEnabled && !DesignerProperties.get_IsInDesignTool())
			{
				image.set_Source(null);
				lock (LowProfileImageLoader._syncBlock)
				{
					LowProfileImageLoader._pendingRequests.Enqueue(new LowProfileImageLoader.PendingRequest(image, uri));
					Monitor.Pulse(LowProfileImageLoader._syncBlock);
				}
				return;
			}
			image.set_Source(new BitmapImage(uri));
		}
		private static void HandleGetResponseResult(IAsyncResult result)
		{
			lock (LowProfileImageLoader._syncBlock)
			{
				LowProfileImageLoader._pendingResponses.Enqueue(result);
				Monitor.Pulse(LowProfileImageLoader._syncBlock);
			}
		}
	}
}
